/*
 * ExpressionView.java
 *
 * Created on Dec 15, 2010, 2:15:20 PM
 *
 * Copyright (c) 2010 Artur Rataj.
 *
 * This code is distributed under the terms of the GNU Library
 * General Public License, either version 2 of the license or, at
 * your option, any later version.
 */

package pl.gliwice.iitis.spatialnn.network.view;

import java.awt.Color;
import java.awt.Graphics2D;
import pl.gliwice.iitis.graphutils.Point;
import pl.gliwice.iitis.graphutils.view.AbstractCentralView;
import pl.gliwice.iitis.graphutils.view.DrawMode;
import pl.gliwice.iitis.graphutils.view.Drawable;

/**
 * A view of an expression level. This only modifies <code>g</code>'s color.<br>
 * The parameter <code>at</code> is meaningless and can be null.
 * 
 * @author Artur Rataj
 */
public class ExpressionView extends AbstractCentralView {
    /**
     * Represents expression value, to be used <i>only</i> with this
     * view.
     */
    public class ExpressionValue implements Drawable {
        public double value;
        
        public ExpressionValue(double value) {
            this.value = value;
        }
    };
    
    /**
     * Creates a new instance of ExpressionView. 
     * 
     * @param dm draw mode
     */
    public ExpressionView(DrawMode dm) {
        super(dm);
    }
    /**
     * Wraps expression value into a new <code>Drawable</code>, to be passed
     * to <code>draw()</code>.
     * 
     * @param value expression value
     * @return drawable object
     */
    ExpressionValue getExpressionValue(double value) {
        return new ExpressionValue(value);
    }
    /**
     * A triangular wave function to convert a color component,
     * used by <code>draw</code>.
     * 
     * @param c input value, >0
     * @return output component value
     */
    protected int wave(int c) {
        if((c&0x100) != 0)
            c = 0xff - (c&0xff);
        else
            c = c&0xff;
        return c;
    }
    @Override
    public void draw(Drawable object, Point at, Graphics2D g) {
        double expression = ((ExpressionValue)object).value;
        int c1 = wave((int)Math.round(expression*1*0xff));
        int c2;
        if(expression < 0.5)
            c2 = wave((int)Math.round(expression*2*0xff));
        else
            c2 = 0xff;
        c2 = 2*c2/3;
        int c3 = wave(0xff - (int)Math.round(expression*3*0xff));
        Color color = new Color(c1, c2, c3);
        g.setColor(color);
    }
}
